<?php

namespace frontend\controllers;

use Yii;

/**
 * Index controller
 */
class IndexController extends IController
{

    public function behaviors()
    {
        return [
            'pageCache' => [
                'class' => 'yii\filters\PageCache',
                'only' => ['index'],
                'duration' => 60,
                'dependency' => [
                    'class' => 'yii\caching\DbDependency',
                    'sql' => 'SELECT COUNT(*) FROM mu_ad_wx',//SELECT COUNT(*) FROM mu_ad_wx
                ],
                'variations' => [
                    \Yii::$app->language,
                    Yii::$app->request->get('channel')
                ]
            ],
        ];
    }

    /**
     * 系统首页
     * @return string
     */
    public function actionIndex()
    {
        $channel = Yii::$app->request->get('channel', 'channel_01');
        if (empty($channel)) {
            $channel = 'channel_01';
        }
        $model = \common\models\base\AdWx::find()
            ->where(['channel' => $channel])
            ->select(['qrcode'])
            ->orderBy(new \yii\db\Expression('Rand()'))
            ->limit(1)->one();
        if (empty($model)) {
            //  return $this->redirect('weixin://contacts/profile/ENAz28--jQzbbm_P_ddVXtE',301);
        }
        return $this->renderPartial('express-ad', ['model' => $model]);
    }

    public function actionTo()
    {
        $domain_list = explode(',', Yii::$app->config->info('JUMP_DOMAIN_LIST'));
        $domains = [];
        foreach ($domain_list as $item) {
            if ($item) {
                $domains[] = $item;
            }
        }
//        $arr = [
//            'aop.xmtaoya.com',
//            'www.3381game.cn',
//            'www.dfcrps.cn',
//            'm.ca81f.cn',
//            'm.te88l.cn'
//        ];
        $channel = Yii::$app->request->get('channel', 'channel_01');
        //$str = Yii::$app->security->generateRandomString(6);
        $count = count($domains);
        $i = random_int(0, ($count - 1));
        $url = "http://{$domains[$i]}?channel={$channel}";
//        print_r($url);exit;
        return $this->redirect($url, 302);
    }


}
